﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace TestApp
{
    /// <summary>
    /// Hyper-V Validation Rules based on the hvRemote script by John Howard
    /// http://code.msdn.microsoft.com/HVRemote
    /// http://blogs.technet.com/b/jhoward/archive/2008/11/14/configure-hyper-v-remote-management-in-seconds.aspx
    /// </summary>
    public partial class HypervDiagnosticToolsForm : Form
    {
        Bitmap imgTick = new Bitmap(@"C:\CodePlex\TFS05\HyperVLib-01\Images\green_tick.png");
        Bitmap imgCross = new Bitmap(@"C:\CodePlex\TFS05\HyperVLib-01\Images\red_cross.png");

        public HypervDiagnosticToolsForm()
        {
            InitializeComponent();
        }

        private void HypervDiagnosticToolsForm_Load(object sender, EventArgs e)
        {
            bwValidator.RunWorkerAsync();
        }

        private void bwValidator_DoWork(object sender, DoWorkEventArgs e)
        {
            int ixValidationRule = 0;
            int total = ValidationRule.AllValidationRules.Count;

            foreach (ValidationRule rule in ValidationRule.AllValidationRules)
            {
                bwValidator.ReportProgress(ixValidationRule * 100 / total, rule); // displays name of rule about to execute
                Thread.Sleep(100); // slows down progress in case rules are very fast; allows for user to notice changing rule names
                
                ValidationResult result = null;
                try
                {
                    result = rule.Check();
                }
                catch (Exception ex)
                {
                    result = new ValidationResult(false) { Message = ex.Message };
                }

                ixValidationRule++;

                bwValidator.ReportProgress(ixValidationRule * 100 / total, result); // displays outcome

                if (!result.Success && result.IsFatal) break;
            }
        }

        private void bwValidator_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState is ValidationRule)
            {
                statusLabel.Text = "Executing Validation Rule: " + ((ValidationRule)e.UserState).Name;
            }
            else if (e.UserState is ValidationResult)
            {
                var result = e.UserState as ValidationResult;
                var success = result.Success;
                var selectedImage = success ? imgTick : imgCross;

                this.SuspendLayout();

                statusLabel.Text = e.ProgressPercentage.ToString() + "%";
                dgResults.Rows.Add(selectedImage, result.Message);

                this.ResumeLayout();
            };
        }

        private void bwValidator_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            statusLabel.Text = string.Empty;
            btnReRun.Enabled = true;
        }

        private void btnReRun_Click(object sender, EventArgs e)
        {
            btnReRun.Enabled = false;
            dgResults.Rows.Clear();
            bwValidator.RunWorkerAsync();
        }
    }
}
